package com.fortune.app.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.fortune.app.R;
import com.fortune.app.adapters.DreamResultAdapter;
import com.fortune.app.models.DreamInterpreter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 解梦Fragment，提供梦境解析功能
 */
public class DreamFragment extends Fragment {
    private EditText etDreamDescription;
    private EditText etKeywords;
    private Button btnInterpret;
    private RecyclerView rvResults;
    private TextView tvNoResults;
    
    private DreamInterpreter dreamInterpreter;
    private DreamResultAdapter resultAdapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dream, container, false);
        
        // 初始化视图
        etDreamDescription = view.findViewById(R.id.et_dream_description);
        etKeywords = view.findViewById(R.id.et_keywords);
        btnInterpret = view.findViewById(R.id.btn_interpret);
        rvResults = view.findViewById(R.id.rv_results);
        tvNoResults = view.findViewById(R.id.tv_no_results);
        
        // 初始化解梦器
        dreamInterpreter = new DreamInterpreter();
        
        // 初始化适配器
        resultAdapter = new DreamResultAdapter(getContext(), new ArrayList<>());
        rvResults.setAdapter(resultAdapter);
        
        // 设置解梦按钮点击事件
        btnInterpret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                interpretDream();
            }
        });
        
        return view;
    }
    
    /**
     * 解析梦境
     */
    private void interpretDream() {
        String dreamDescription = etDreamDescription.getText().toString().trim();
        String keywordsText = etKeywords.getText().toString().trim();
        
        if (dreamDescription.isEmpty()) {
            etDreamDescription.setError("请输入梦境描述");
            return;
        }
        
        // 解析关键词
        List<String> keywords = new ArrayList<>();
        if (!keywordsText.isEmpty()) {
            String[] keywordArray = keywordsText.split("，|,");
            for (String keyword : keywordArray) {
                if (!keyword.trim().isEmpty()) {
                    keywords.add(keyword.trim());
                }
            }
        }
        
        // 解析梦境
        Map<String, Object> result = dreamInterpreter.interpretDream(dreamDescription, keywords);
        
        // 显示结果
        if (result.isEmpty()) {
            tvNoResults.setVisibility(View.VISIBLE);
            rvResults.setVisibility(View.GONE);
        } else {
            tvNoResults.setVisibility(View.GONE);
            rvResults.setVisibility(View.VISIBLE);
            
            // 更新适配器数据
            resultAdapter.updateData(result);
        }
    }
}
